// $URL: http://mingw-lib.googlecode.com/svn/trunk/working/nam/nam.h $
// $Rev: 683 $
// $Author: akio.miyoshi $
// $Date:: 2012-06-16 09:31:46 +0000#$

#ifndef NETWORKACCESSMANAGER_H
#define NETWORKACCESSMANAGER_H

#include <QtCore>
#include <QtNetwork>

#include "ntr.h"

class NetworkThreadRequest;
class NetworkAccessBackendFactory;

class NetworkAccessManager : public QNetworkAccessManager
{
	Q_OBJECT

public:
	NetworkAccessManager(NetworkThreadRequest *a_independent = 0, QObject *parent = 0);
	void addToSilentList(QString v_url_begin);
	void addToSilentList(QRegExp *v_url_rx);
	bool isInSilentList(QUrl v_url);
	virtual QNetworkReply *createRequest (
			Operation op,
			const QNetworkRequest & req,
			QIODevice * outgoingData = 0
//#ifdef QT_BACKEND_BUILD
//			, QObject *backend = 0
//#endif
			);
	void setBackendFactory(NetworkAccessBackendFactory *a_factory);
	QByteArray doGet(QString a_url, QString *a_contentType = 0);
	QByteArray doGet(QUrl a_url, QString *a_contentType = 0);
	QByteArray doGet(QNetworkRequest request, QString *a_contentType = 0);
	QByteArray doPost(QNetworkRequest request, QByteArray postData);
	QMap<QByteArray, QByteArray> doGetHead(QNetworkRequest request);
	int batDelete(QNetworkRequest request);
	int batPost(QNetworkRequest request, QByteArray &postData, QByteArray &responsdData);

private:
	NetworkAccessBackendFactory *m_factory;
	NetworkThreadRequest *m_independent;
	QMutex m_mutex;
	QStringList m_silentList;
	QList<QRegExp *> m_silentRx;

private slots:
	void authenticationRequired(QNetworkReply *reply, QAuthenticator *auth);
	void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *auth);
#ifndef QT_NO_OPENSSL
	void sslErrors(QNetworkReply *reply, const QList<QSslError> &error);
#endif
	void requestFinished(QNetworkReply *reply);
};

class NetworkAccessBackendFactory : public QObject
{
	Q_OBJECT
public:
	NetworkAccessBackendFactory(QObject *parent = 0) {}
	virtual QObject *create(QNetworkAccessManager::Operation op,
							const QNetworkRequest &request) const
	{ return 0; }
};

#endif // NETWORKACCESSMANAGER_H
